package com.uday.kraterchat.chatclient.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.uday.kraterchat.chatclient.business.UserServiceDelegate;
	import com.uday.kraterchat.chatclient.control.LogoutUserEvent;
	import com.uday.kraterchat.chatclient.control.SubscribeMsgEvent;
	import com.uday.kraterchat.chatclient.data.MessageItem;
	import com.uday.kraterchat.chatclient.data.MessageType;
	import com.uday.kraterchat.chatclient.data.UserInfoBaseItem;
	import com.uday.kraterchat.chatclient.model.ChatClientModel;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.messaging.events.MessageEvent;
	import mx.rpc.IResponder;

	public class SubscribeMsgCommand implements IResponder, ICommand
	{
		private var objChatClientModel:ChatClientModel = ChatClientModel.getInstance();
		
		public function SubscribeMsgCommand()
		{
		}

		public function result(event:Object):void
		{
			var objMessageItem:MessageItem = (event as MessageEvent).message.body as MessageItem;
			
			if(objMessageItem.strMessageType == MessageType.FRIEND_LIST){
				objChatClientModel.lstFriends = objMessageItem.lstDataItems;
				//objChatClientModel.strRecvdMessages += "\nFriend List refreshed at " + new Date().toString();
			} else if(objMessageItem.strMessageType == MessageType.LOGOUT_MESSAGE){
				logoutUser();
			} else{
				handleChatMessage(objMessageItem);
			}
		}
		
		public function fault(info:Object):void
		{
			Alert.show("Error getting friends list.");
		}
		
		public function execute(event:CairngormEvent):void
		{
			var objLoginUserDel:UserServiceDelegate = new UserServiceDelegate(this);
			if((event as SubscribeMsgEvent).subscribe){
				objLoginUserDel.subsribeMessages((event as SubscribeMsgEvent).strDestination);
			} else{
				objLoginUserDel.unSubsribeMessages();
			}
		}
		
		private function handleChatMessage(objMessageItem:MessageItem):void{
			var objUserInfo:UserInfoBaseItem = null;
			var lstFriends:ArrayCollection = objChatClientModel.lstFriends;
			
			for(var index:int;index < lstFriends.length;index++){
				objUserInfo = lstFriends.getItemAt(index) as UserInfoBaseItem;
				
				if(objUserInfo.lngUserID == objMessageItem.lngFrmUserId){
					break;
				}
			}
			
			//If the sender is found on the friend list open the window.
			if(objUserInfo && (index != objChatClientModel.lstFriends.length)){
				objChatClientModel.objChatWindow.routeInMsgs(objUserInfo,objMessageItem);
			}
			
			if((objMessageItem.strMessageType == MessageType.FILE_TRANSFER) || 
				(objMessageItem.strMessageType == MessageType.CHAT_MESSAGE)){
				Application.application.notifyUser();
			}
			
			//objChatClientModel.strRecvdMessages += objMessageItem.lstDataItems.getItemAt(0).toString();
		}
		
		private function logoutUser():void{
			//Unsubscribe from the feed
/* 			var objLoginUserDel:UserServiceDelegate = new UserServiceDelegate(this);
			objLoginUserDel.unSubsribeMessages();
			objChatClientModel.mainAppState = KraterChatAir.STATE_LOGIN; */
			CairngormEventDispatcher.getInstance().dispatchEvent(new LogoutUserEvent(objChatClientModel.objCurrUser));
		}
		
	}
}